package com.slkj.shilixiaoyuanapp.ui.common.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.zhouzhuo.zzletterssidebar.adapter.BaseSortRecyclerViewAdapter;
import me.zhouzhuo.zzletterssidebar.viewholder.BaseRecyclerViewHolder;

/**
 * Created  on 2018-08-21
 *
 * @author 唐开阔
 * @describe 发布对象适配器
 */
public class SendObjAdapter extends BaseSortRecyclerViewAdapter<PeopleEntity,BaseRecyclerViewHolder> {
    public static final int TYPE_LEVEL_0 = 1;
    public static final int TYPE_PERSON = 2;
    Context context ;
    ArrayList<PeopleEntity> data;
    private Map<String,Integer> mapLetter;

    public SendObjAdapter(Context context, ArrayList<PeopleEntity> data) {
        super(context,data);
        this.context = context;
        this.data = data;
    }

    //return your itemView layoutRes id
    @Override
    public int getItemLayoutId() {
        return R.layout.item_chose_people;
    }

    //add a header ,optional, if no need, return 0
    @Override
    public int getHeaderLayoutId() {
        return R.layout.item_chose_obj_head;
    }

    //add a footer, optional, if no need, return 0
    @Override
    public int getFooterLayoutId() {
        return 0;
    }

    @Override
    public BaseRecyclerViewHolder getViewHolder(View itemView, int type) {
        switch (type) {
            case BaseSortRecyclerViewAdapter.TYPE_HEADER:
                return new GroupViewHolder(itemView);
        }
        return new PeopleViewHolder(itemView);
    }

    public ArrayList<PeopleEntity> getData() {
        return data;
    }

    public void setData(ArrayList<PeopleEntity> data) {
        this.data = data;
    }

    //    public SendObjAdapter(ArrayList<PeopleEntity> data, Context context) {
//        super(data);
//        this.context = context;
//        addItemType(TYPE_LEVEL_0, R.layout.item_chose_people);
//        addItemType(TYPE_PERSON, R.layout.item_chose_obj_head);
//    }
//    protected void convert(final BaseViewHolder helper, final MultiItemEntity item) {
//        final PeopleEntity entity = (PeopleEntity) item;
//
//        switch (helper.getItemViewType()) {
//            case TYPE_LEVEL_0:
//                helper.setText(R.id.tv_name,entity.getUName());
//                break;
//            case TYPE_PERSON:
//                helper.setText(R.id.tv_name,entity.getUName());
//                ImageView imgCheck = helper.getView(R.id.img_head);
//                ImageView imgUser = helper.getView(R.id.img_user);
//                Glide.with(context).load(entity.getHeadImg())
//                        .error(R.drawable.ic_man)
//                        .fallback(R.drawable.ic_man)
//                        .into(imgUser);
//                imgCheck.setImageResource(entity.isCheck() ? R.drawable.ic_checked :R.drawable.ic_un_checked);
//                helper.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        entity.setCheck(!entity.isCheck());
//                        imgCheck.setImageResource(entity.isCheck() ? R.drawable.ic_checked :R.drawable.ic_un_checked);
//                    }
//                });
//                break;
//        }
//    }


    @NonNull
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_LEVEL_0:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chose_obj_head, parent, false);
                return new GroupViewHolder(view);
            case TYPE_PERSON:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chose_people, parent, false);
                return new PeopleViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseRecyclerViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_0:
               GroupViewHolder groupViewHolder = (GroupViewHolder) holder;
                groupViewHolder.bind(position, data.get(position));
                break;
            case TYPE_PERSON:
            PeopleViewHolder peopleViewHolder = (PeopleViewHolder) holder;
                peopleViewHolder.bind(position, data.get(position));
                break;
        }
    }



    @Override
    public int getItemViewType(int position) {
        return data.get(position).getItemType();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class GroupViewHolder extends BaseRecyclerViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;

        View itemView;

        public GroupViewHolder(View View) {
            super(View);
            itemView = View;
            ButterKnife.bind(this, View);
        }

        @SuppressLint("SetTextI18n")
        public void bind(int pos, PeopleEntity groupEntity) {
            tvName.setText(groupEntity.getUName());
        }
    }

    class PeopleViewHolder extends BaseRecyclerViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.img_head)
        ImageView imgCheck;

        @BindView(R.id.img_user)
        ImageView imgUser;
        View itemView;
        public PeopleViewHolder(View view) {
            super(view);
            itemView = view;
            ButterKnife.bind(this, view);
        }
        public void bind(int pos, PeopleEntity person) {
            tvName.setText(person.getUName());
            Glide.with(context).load(person.getHeadImg())
                    .error(R.drawable.ic_man)
                    .fallback(R.drawable.ic_man)
                    .into(imgUser);
            imgCheck.setImageResource(person.isCheck() ? R.drawable.ic_checked :R.drawable.ic_un_checked);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    person.setCheck(!person.isCheck());
                    imgCheck.setImageResource(person.isCheck() ? R.drawable.ic_checked :R.drawable.ic_un_checked);
                }
            });
        }

    }
}
